//
//  CTUserWebView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/18.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserWebView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTActivitorView.h"
#import "CTDeviceFunction.h"
#import "CTAppLanguage.h"
#import <WebKit/WebKit.h>


@interface CTUserWebView ()<UIWebViewDelegate,WKNavigationDelegate>
{
    UIView * topView;
    CTActivitorView * activitorView;
    
    UIWebView * activityWebView;
    WKWebView * wkWebView;
}

@end

@implementation CTUserWebView
@synthesize delegate;

- (void)dealloc
{
    [topView removeFromSuperview];
    topView = nil;
    [activityWebView removeFromSuperview];
    activityWebView = nil;
    activityWebView.delegate = nil;
    delegate = nil;
    [activitorView removeFromSuperview];
    activitorView = nil;
    [wkWebView removeFromSuperview];
    wkWebView = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTUserWebView Is Removed!!!"];
}

- (instancetype)initWithType:(CTUserWebViewType)type
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        [self creatSubviewsWithType:type];
    }
    return self;
}


-(void)creatSubviewsWithType:(CTUserWebViewType)type
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(0,63.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topLineView.backgroundColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    [topView addSubview:topLineView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    if (type == CTUserWebViewTypeVerifiedWeibo) {
         titleLabel.text = [CTAppLanguage localizedStringForKey:@"userWebView_weibo" withValue:nil withTable:@"Language"];
    }else{
        titleLabel.text = [CTAppLanguage localizedStringForKey:@"userWebView_shop" withValue:nil withTable:@"Language"];
    }
   
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:19];
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"biaotilan_bt_back"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
    
    
    if ([CTDeviceFunction deviceSystemVersion] == DeviceSystemVersion_iOS8){
        wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64)];
        wkWebView.navigationDelegate = self;
        [self addSubview:wkWebView];
    }
    else {
        activityWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64)];
        activityWebView.delegate = self;
        [self addSubview:activityWebView];
    }
    
}

#pragma mark - action & delegate -

-(void)backButtonAction:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(backAction:)]) {
        [self.delegate backAction:self];
    }
}


#pragma mark - wk web delegate -

-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    [activitorView removeMoeMarkActivitorView];
    activitorView = nil;
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [activitorView removeMoeMarkActivitorView];
    activitorView = nil;
}


#pragma mark - web delegate -


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activitorView removeMoeMarkActivitorView];
    activitorView = nil;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [activitorView removeMoeMarkActivitorView];
    activitorView = nil;
}

#pragma mark - setter -

-(void)setUrl:(NSString *)loadUrl
{
    activitorView = [[CTActivitorView alloc] init];
    [activitorView showMoeMarkActivitorViewOn:self];
    
    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:loadUrl]cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:CT_HEADER_REQUEST_TIMEOUT_INTERVAL];
    
    if ([CTDeviceFunction deviceSystemVersion] == DeviceSystemVersion_iOS8)[wkWebView loadRequest:request];
    else [activityWebView loadRequest:request];
    
}



@end
